/* 
 *  Copyright (c) 2012 Shirou Maruyama
 * 
 *   Redistribution and use in source and binary forms, with or without
 *   modification, are permitted provided that the following conditions
 *   are met:
 * 
 *   1. Redistributions of source code must retain the above Copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above Copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 *   3. Neither the name of the authors nor the names of its contributors
 *      may be used to endorse or promote products derived from this
 *      software without specific prior written permission.
 */

#ifndef PACBEEP_HPP_
#define PACBEEP_HPP_

#include <vector>
#include <iostream>
#include <deque>
#include <stdint.h>
#include "dictionary.hpp"

namespace pacbeep {

const uint32_t kPrime = 66666667UL;
const uint32_t kMaxQueLen = 8UL;
const uint32_t kMaxNumQue = 64UL;

typedef struct node__ {
  node__(): code(kDummy), val(kDummy), len(0) {}
  node__(uint8_t ch): code(ch), val(ch), len(1) {}
  uint32_t code;
  uint64_t val;
  uint32_t len;
} NODE;
 
class PACBEEP {
 public:
  PACBEEP(): Queues_(), NumQue_(0), NumRules_(0), Dict_() {}
  void Init(const uint32_t k);
  void InsertChar(const uint8_t ch, std::ostream& os);
  void Flush(std::ostream& os);
  uint32_t NumRule() const { return NumRules_; }
  
 private:
  std::vector<std::deque<NODE> > Queues_;
  uint32_t NumQue_;
  uint32_t NumRules_;
  DICT Dict_;

  bool IsRepetition(uint32_t level, uint32_t pos) const;
  bool IsMinimal(const uint32_t level, const uint32_t pos) const;
  bool IsMaximal(const uint32_t level, const uint32_t pos) const;
  bool IsPair(const uint32_t level) const;
  void GrammarTrans(const NODE& node, uint32_t level, std::ostream& os);
  NODE ReplaceCodes(const NODE& left, const NODE& right, std::ostream& os);

}; // class PACBEEP

} // namespace pacbeep

#endif // PACBEEP_HPP_
